package com.tanhua.server.controller;

import com.tanhua.server.service.MovementsService;
import com.tanhua.server.service.QuanziMQService;
import com.tanhua.server.vo.Movements;
import com.tanhua.server.vo.PageResult;
import com.tanhua.server.vo.VisitorsVo;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

@RestController
@RequestMapping("movements")
public class MovementsController {

    @Autowired
    private MovementsService movementsService;

    @Autowired
    private QuanziMQService quanziMQService;

    /**
     * 发布动态  使用POST提交
     *
     * @param textContent 发布文本
     * @param location    位置
     * @param files       文件 图片
     * @param longitude   经度
     * @param latitude    纬度
     * @return
     */
    @PostMapping()
    public ResponseEntity<Void> savePublish(@RequestParam(value = "textContent", required = false) String textContent,
                                            @RequestParam(value = "location", required = false) String location,
                                            @RequestParam(value = "imageContent") MultipartFile[] files,
                                            @RequestParam("longitude") String longitude,
                                            @RequestParam("latitude") String latitude
    ) {

        //调用service方法
        String publishId = movementsService.savePublish(textContent, location, files, longitude, latitude);

        try {
            if (StringUtils.isNotEmpty(publishId)) {
                //发送消息
                quanziMQService.publishMsg(publishId);
                return ResponseEntity.ok(null);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();

    }


    /**
     * 查询好友动态
     *
     * @param page     页码
     * @param pageSize 条目数
     * @return
     */
    @GetMapping()
    public PageResult queryPublishList(@RequestParam(value = "page", defaultValue = "1") Integer page,
                                       @RequestParam(value = "pagesize", defaultValue = "10") Integer pageSize) {

        return movementsService.queryUserPublishList(page, pageSize);

    }


    /**
     * 查询推荐动态
     *
     * @param page
     * @param pageSize
     * @return
     */
    @GetMapping("recommend")
    public ResponseEntity<PageResult> queryRecommendPublishList(@RequestParam(value = "page", defaultValue = "1") Integer page,
                                                                @RequestParam(value = "pagesize", defaultValue = "10") Integer pageSize) {

        try {
            PageResult pageResult = movementsService.queryRecommendPublishList(page, pageSize);
            return ResponseEntity.ok(pageResult);
        } catch (Exception e) {
            e.printStackTrace();
        }

        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();

    }


    /**
     * 点赞
     *
     * @param publishId 动态id
     * @return
     */
    @GetMapping("/{id}/like")
    public ResponseEntity<Long> saveLikeComment(@PathVariable("id") String publishId) {

        try {
            Long likeComment = movementsService.saveLikeComment(publishId);
            //如果返回的likeComment不为空,则点赞成功
            if (likeComment != null) {
                //发送点赞消息
                this.quanziMQService.likePublishMsg(publishId);


                return ResponseEntity.ok(likeComment);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
    }


    /**
     * 取消点赞
     *
     * @param publishId 动态id
     * @return
     */
    @GetMapping("/{id}/dislike")
    public ResponseEntity<Long> disLikeComment(@PathVariable("id") String publishId) {
        try {
            Long likeComment = movementsService.cancelLikeComment(publishId);
            //如果返回的likeComment不为空,则取消点赞成功
            if (likeComment != null) {
                //发送取消点赞消息
                this.quanziMQService.disLikePublishMsg(publishId);

                return ResponseEntity.ok(likeComment);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
    }


    /**
     * 喜欢
     *
     * @param publishId 动态id
     * @return
     */
    @GetMapping("/{id}/love")
    public ResponseEntity<Long> saveLoveComment(@PathVariable("id") String publishId) {
        try {
            Long loveComment = movementsService.saveLoveComment(publishId);
            //如果返回的likeComment不为空,则点赞成功
            if (loveComment != null) {
                //发送喜欢消息
                this.quanziMQService.lovePublishMsg(publishId);

                return ResponseEntity.ok(loveComment);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
    }


    /**
     * 取消喜欢
     *
     * @param publishId
     * @return
     */
    @GetMapping("/{id}/unlove")
    public ResponseEntity<Long> disLoveComment(@PathVariable("id") String publishId) {
        try {
            Long loveCount = this.movementsService.cancelLoveComment(publishId);
            if (null != loveCount) {
                //发送取消喜欢消息
                this.quanziMQService.disLovePublishMsg(publishId);

                return ResponseEntity.ok(loveCount);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
    }


    /**
     * 查询单条动态
     *
     * @param publishId
     * @return
     */
    @GetMapping("{id}")
    public ResponseEntity<Movements> querypublishById(@PathVariable("id") String publishId) {

        try {
            Movements movements = movementsService.querypublishById(publishId);
            if (null != movements) {

                //发送消息
                this.quanziMQService.queryPublishMsg(publishId);
                return ResponseEntity.ok(movements);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
    }


    /**
     * 谁看过我
     *
     * @return
     */
    @GetMapping("visitors")
    public ResponseEntity<List<VisitorsVo>> queryVisitorsList() {
        try {
            List<VisitorsVo> list = this.movementsService.queryVisitorsList();
            return ResponseEntity.ok(list);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
    }



    /**
     * 自己的所有动态
     *
     * @return
     */
    @GetMapping("all")
    public ResponseEntity<PageResult> queryAlbumList(@RequestParam(value = "page", defaultValue = "1") Integer page,
                                                     @RequestParam(value = "pagesize", defaultValue = "10") Integer pageSize,
                                                     @RequestParam(value = "userId") Long userId) {
        try {
            PageResult pageResult = this.movementsService.queryAlbumList(userId, page, pageSize);
            return ResponseEntity.ok(pageResult);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
    }


}
